from flask import Flask,redirect,render_template, request,json,jsonify,url_for,current_app,Blueprint
from .utils import *
from .model import peaktasklog

webapp = Blueprint('webapp', __name__,template_folder='../web/template',static_url_path='',static_folder='../web/static')

@webapp.route('/',methods=['GET'])
def index():
    return render_template('index.html')

@webapp.route('/tasklog',methods=['POST'])
def tasklogs():
    tid = request.values.get('tid',0)
    tasklogs = []
    try:
        for item in peaktasklog.select().where(peaktasklog.taskid == tid).order_by(peaktasklog.addtime.desc()).limit(4):
            addtime = item.addtime.strftime('%m/%d %H:%M:%S')
            item.addtime = addtime
            tasklogs.append(model_to_dict(item))
    except Exception as ex:
        print(ex)
        pass
    return jsonify(code=1,msg='日志列表',list=tasklogs)

@webapp.route('/tasklist',methods=['POST'])
def tasklist():
    tasks = []
    try:
        for task in reloadtasks(current_app.apscheduler):
            item = {}
            item['title'] = task.title
            item['nexttime'] = task.nexttime.strftime('%m/%d %H:%M:%S')
            item['name'] = task.name
            item['interval'] = task.interval
            item['id'] = task.id
            item['remark'] = task.remark
            item['status'] = task.status
            tasks.append(item)
    except Exception as ex:
        print(ex)
        pass
    return jsonify(code=1,msg='任务列表',list=tasks)


